#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from app import App
from utils.tools import Tools
from utils.element import Element
import re as osRe

import gol
gol._init()
from os import popen as osPopen, path as osPath  # 管道处理

# app基本信息
oAppInfo = {}

class Application(App):
    # 初始配置
    def __init__(self):
        super().__init__()
        global oAppInfo
        oAppInfo = self._d.info

    # 首页弹窗
    def closeAdPopup(self):
        bNode, oNode = self.tapNode("id", "com.jd.jdlite:id/close")
        if (bNode):
            self.myEcho("关闭首页弹窗", bNode)

    # 首页 -> 百元生活费按钮
    def getBYSHF(self): 
        bNode, oNode = self.tapNode("d", "赚钱")
        if (bNode):
            self.myEcho("百元生活费 - tabs", bNode)
            self.tool.setSleep(5)

    # 签到领现金 - 赚金币大按钮(开启抽屉菜单)
    def getZJB(self):
        self.tapNode("id", "com.jd.jdlite.lib.mission.feature:id/cc")

    # ////////-----------------

    # 滚动217个像素
    def moveUp217(self):
        xy = self._d.window_size()
        center = xy[0] // 2
        bottom = xy[1] - 200
        tupMove = (center, bottom, center, bottom - 217)
        self.tool.move(tupMove)
        self.tool.setSleep(2)


    # 迭代器oth
    def othItem(self):
        tupList = (
            "邀请抽奖", "开店赚大钱", "逛特价团爆款", "分亿万京豆抵钱花", "签到领现金",
            "签到领现金", "包裹的旅行", "去京东金融养小猪", "签到免单",
            # "京东健康多点实在", "福袋大赢家", 
            # "看视频赚金币", "邀好友赚金币","邀好友送现金", "汪汪乐园", "领点点券兑红包"
            # "购券抽大奖", "幸运大转盘",  "水果免费领", "逛天天特价爆款",
            #  "汪汪赛跑", "天天抽奖", "瓜分10万红包", "推推赚大钱", "东东爱消除", "发财挖宝",
            # "超值低价购", "看直播 赢好礼", "砍价免费拿",  "新人特价购", "签到领话费礼包",  "分享赚金币", "发财大赢家", "投胎模拟器",
            # "赏金猎人", "京东支付省钱券", "京东新百货", "穿行寻宝", 
        )
        itGo = iter(tupList)
        for tupItem in itGo:
            bNode, oNode = self.tapNode("t", tupItem)
            if (bNode):
                # self.tool.setSleep(1)
                self._d.press("back")
                self.tapNode('t', '残忍离开')
                self.tool.setSleep(2)
            self.moveUp217()

    # ////////-----------------


    # 抽屉 前三个 ---------------
    # 抽屉 - 逛商品去赚钱
    def preItem(self, iIndex):
        oList = { 'gsp': "逛商品赚金币", 'ghd': "点击浏览，赚大额金币" }
        bNode = self._d(text=oList[iIndex]).sibling(text="去赚钱").click_exists(timeout=2)
        self.myEcho(oList[iIndex], bNode)
        return bNode

    # 抽屉 - 活动展示
    def showActivity(self):
        self.tool.setSleep(4)
        self._d.swipe(500, 1500, 870, 280, 0.2)
        self.tool.setSleep(4)
        self._d.swipe(300, 1580, 790, 320, 0.5)
        self.tool.setSleep(4)
        self._d.swipe(300, 1580, 690, 330, 0.5)
        self.tool.setSleep(2)
        # self._d.swipe(300, 1580, 800, 310, 0.5)
        # self.tool.setSleep(5)


    def activityAll(self):
        bNode = self._d(text="去赚钱").click_exists(timeout=2)
        return bNode


    # 抽屉 - 循环活动
    def forActivity(self, click):
        i = 0
        isNext = True
        while isNext == True:
            i += 1
            if (click):
                isNext = self.showActivity()
                if (i == 2):
                    self._d.press("back")
            else:
                self._d.swipe(500, 1500, 870, 300, 0.2)
                self.tool.setSleep(3,6)
            # 无法判断是否完成。加个验证
            # if (i > 40 and self._d(text='今日已完成').exists): 
            #     isNext = False


    # 领红包
    def lingHongBao(self):
        bNode = self.el2CE('t', '领红包')
        if (bNode):
            self.tool.setSleep(4)
            self._d.click(0.5, 0.47)
            self.tool.setSleep(3)
            self._d.click(0.5, 0.47)
            self.tool.setSleep(3)
            self._d.click(0.5, 0.47)
            self.tool.setSleep(3)
            self._d.press("back")
            self.tool.setSleep(2)
        self.el2CE('t', '签到提现')
        self.tool.setSleep(6)
        self._d.press("back")

    # ////////-----------------
    def getWHCenter(self):
        global oAppInfo
        y = oAppInfo.displayHeight // 2
        x = oAppInfo.displayWidth // 2
        return (x, y)

    def startApp(self):
        self.tool.callApp('com.jd.jdlite/com.jd.jdlite.MainActivity')


    def killApp(self):
        self._d.app_stop_all()
        self.tool.setSleep(5)


class Action(Application):
    # 初始配置
    def __init__(self):
        super().__init__()

    def start(self):
        self.startApp()
        self.closeAdPopup()
        # 百元生活费
        self.getBYSHF()
        # 开启抽屉
        self.getZJB()
        self.tool.setSleep(1)

    def doNext(self, sAction):
        # 启动
        if (self.bStart):
            self.start()
        while(self.preItem(sAction)):
            self.forActivity(True)
            self._d.press("back")

    def doOth(self):
        # 启动
        if (self.bStart):
            self.start()
        # self.moveUp217()
        self.moveUp217()
        self.othItem()

    def init(self):
        sAction = self.sAction
        if (sAction == 'all'):
            self.doNext('gsp')
            try:
                self.doNext('ghd')
            except:
                self.doNext('ghd')
            self.doOth()
        elif (sAction in 'gsp|ghd|ksp'):
            self.doNext(sAction)
        elif (sAction=='oth'):
            self.doOth()
        elif (sAction == 'lhb'):
            self.lingHongBao()
        elif (sAction=='start'):
            try:
                self.startApp()
                print("%s - 成功" %('APP启动'))
            except:
                print("%s - 失败" %('APP启动'))
        else:
            print('test')
            # self.lingHongBao()


action = Action()
action.init()
print('DONE')
